#include<iostream>
#include <unistd.h>
#include <sys/types.h>
 #include <unistd.h>
 #include <cstdio>
 #include<cstring>
 #include <sys/wait.h>
using namespace std;
int fds[2]={0};
void cwrite(){
    int n=10;
    char bf[1024]={0};
    while(n--){
        snprintf(bf,sizeof(bf),"我是子进程 : pid:%d count:%d",getpid(),n);
        write(fds[1],bf,strlen(bf));
       sleep(1);
    }
}
void fread(){
    int n=10;
    char bf[1024]={0};
    while(n--){
        read(fds[0],bf,sizeof(bf)-1);
        cout<<"父进程收到的子进程信息:"<<bf<<endl;
    }
}

int main(){
   
   int m=pipe(fds);
   if(m<0) cerr<<"pipe 错误"<<endl;
   //else{ cout<<fds[0]<<" "<<fds[1]<<endl;}
   pid_t pid=fork();
   //子进程：
   if(pid==0){
     close(fds[0]);
     cwrite();
     close(fds[1]);
     exit(0);
   }
   //父进程：
   close(fds[1]);
   fread();
waitpid(pid, nullptr, 0);
close(fds[0]);
  
return 0;
 
}